package com.cl.security.system.entity;

import com.cl.security.base.entity.BaseEntity;
import com.fasterxml.jackson.annotation.JsonBackReference;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
*
* @author chenlong
* @date 2020/12/8
*/
@Getter
@Setter
@Entity
@Table
@SQLDelete(sql = "update role set deleted=1 where id=?")
@Where(clause = "deleted<>1")
public class Role extends BaseEntity {
    @JsonBackReference
    @ManyToMany(mappedBy = "roles")
    private Set<User> users = new HashSet<>();

    @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinTable(name = "role_permission", joinColumns = @JoinColumn(columnDefinition = "role_id"), inverseJoinColumns = @JoinColumn(columnDefinition = "permission_id"))
    private Set<Permission> permissions = new HashSet<>();
    @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinTable(name = "role_menu", joinColumns = @JoinColumn(columnDefinition = "role_id"), inverseJoinColumns = @JoinColumn(columnDefinition = "menu_id"))
    private Set<Menu> menus = new HashSet<>();
}
